<template>
  <view>
    <uni-popup ref="popup" type="right" :is-mask-click="false">
      <view class="filter-scroll-view">
        <view class="filter-mask" @click="toggle"></view>
        <view>
          <scroll-view class="scroll-view-box" scroll-y="true">
            <u-gap bgColor="#fff" height="20"></u-gap>
            <view class="cu-filter-item">
              <view class="title">项目名称</view>
              <view class="cu-filter-control">
                <u-input placeholder="输入作业项目名称进行检索" v-model="searchData.projectName" />
              </view>
            </view>

            <view class="cu-filter-item" style="padding-left: 30rpx">
              <cu-cell title="所属区域" link :description="searchData.areaName" @click="showFactoryPicker"> </cu-cell>
            </view>
            <view class="cu-filter-item" style="padding-left: 30rpx">
              <cu-cell title="所属部门" link :description="searchData.deptName" @click="showDeptPicker"> </cu-cell>
            </view>

            <cu-radio title="审批状态" :dictList="checkStatusOptions" v-model="searchData.checkStatus" />
            <cu-radio title="项目进度" :dictList="workProgressOptions" v-model="searchData.workProgress" />

            <filter-time title="项目日期" v-model="searchData.date" />
          </scroll-view>
          <view class="btn-wrap">
            <view class="reset" @click="resetHandler">重置条件</view>
            <view class="confirm" @click="submitHandler">搜索</view>
          </view>
        </view>
      </view>
    </uni-popup>

    <cu-tree-select
      ref="areaSelectRef"
      :checkList="areaCheckList"
      :request-api="getFactoryArea"
      label-key="areaName"
      value-key="areaId"
      children-key="childList"
      @change="onAreaChange"
    />

    <cu-tree-select
      ref="deptSelectRef"
      :checkList="deptCheckList"
      :request-api="listDeptChildren"
      label-key="label"
      value-key="id"
      children-key="children"
      @change="onDeptChange"
    />
  </view>
</template>

<script>
import CuCell from '@/components/cu-cell/cu-cell.vue'
import UInput from '@/uni_modules/uview-ui/components/u--input/u--input.vue'
import CuTreeSelect from '@/components/cu-tree-select/cu-tree-select.vue'
import { getFactoryArea } from '@/api/spw/project'
import FilterTime from './FilterTime.vue'
import CuRadio from './CuRadio.vue'
import { listDeptChildren } from '@/api/system/dept'
export default {
  components: {
    FilterTime,
    CuRadio,
    CuTreeSelect,
    UInput,
    CuCell
  },
  data() {
    return {
      areaCheckList: [],
      deptCheckList: [],
      workProgressOptions: [
        {
          dictValue: '1',
          dictLabel: '未启动'
        },
        {
          dictValue: '2',
          dictLabel: '进行中'
        },
        {
          dictValue: '3',
          dictLabel: '已完成'
        }
      ],
      checkStatusOptions: [
        {
          dictValue: '1',
          dictLabel: '待审批'
        },
        {
          dictValue: '2',
          dictLabel: '审批通过'
        },
        {
          dictValue: '3',
          dictLabel: '审批不通过'
        }
      ],
      searchData: {
        projectName: '',
        workProgress: undefined,
        checkStatus: undefined,
        deptName: undefined,
        deptId: undefined,
        areaName: undefined,
        areaId: undefined,
        date: []
      }
    }
  },
  props: {
    id: {
      type: [Number, Array, String]
    },
    showArr: {
      type: Array,
      default: () => []
    }
  },

  methods: {
    listDeptChildren,
    getFactoryArea,
    showFactoryPicker() {
      this.$refs.areaSelectRef.open()
    },
    showDeptPicker() {
      this.$refs.deptSelectRef.open()
    },
    onAreaChange(val) {
      this.areaCheckList = val
      this.searchData.areaId = val[0].areaId
      this.searchData.areaName = val[0].areaName
    },
    onDeptChange(val) {
      this.deptCheckList = val
      this.searchData.deptId = val[0].id
      this.searchData.deptName = val[0].label
    },
    async toggle() {
      if (this.show) {
        this.show = false
        this.showTime = false
        this.$refs.popup.close()
      } else {
        this.show = true
        this.$refs.popup.open()
        // 时间组件加载耗时，延迟加载
        this.$nextTick(() => {
          setTimeout(() => {
            this.showTime = true
          }, 100)
        })
      }
    },
    resetHandler() {
      this.areaCheckList = []
      this.deptCheckList = []
      this.searchData = {
        projectName: '',
        workProgress: undefined,
        checkStatus: undefined,
        deptName: undefined,
        deptId: undefined,
        areaName: undefined,
        areaId: undefined,
        date: []
      }
      this.submitHandler()
    },
    submitHandler() {
      this.$emit('submit', this.searchData)
      this.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-scroll-view {
  width: 100vw;
  height: 100vh;
  display: flex;

  .filter-mask {
    flex: 1;
  }

  .scroll-view-box {
    width: 672rpx;
    background: #fff;
    box-sizing: border-box;
    height: calc(100vh - 220rpx);
    /* #ifdef APP-PLUS */
    height: calc(100vh - 140rpx);
    /* #endif */
  }

  .btn-wrap {
    background: #fff;
    display: flex;
    justify-content: space-around;
    padding-top: 30rpx;
    padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(30rpx + env(safe-area-inset-bottom));

    .reset,
    .confirm {
      width: 292rpx;
      line-height: 80rpx;
      background: #ffffff;
      font-size: 32rpx;
      text-align: center;
    }

    .reset {
      color: #666666;
      background: #ffffff;
      border: 1rpx solid #cccccc;
    }

    .confirm {
      background: #2ac0c8;
      color: #fff;
    }
  }
}

.cu-filter-item {
  padding-bottom: 40rpx;
  .title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333333;
    padding: 0 0 20rpx 30rpx;
  }
  .cu-filter-control {
    padding: 0 30rpx;
  }
}

.ticketCode {
  margin-bottom: 25rpx;
  // margin-top: 25rpx;
  padding: 0 15px;

  .cu-cell__item {
    margin-top: -13px;
  }

  .title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333333;
    padding: 0 0 20rpx 30rpx;
  }

  .input {
    margin-left: 35rpx;
    width: 90%;
  }
}
::v-deep .cu-filter-item .cu-cell__item .cell-item__title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
}
</style>
